#include "chooselevelscene.h"
#include "ui_chooselevelscene.h"
#include "QPainter"
#include "mypushbutton.h"
#include "QTimer"
#include "QLabel"
#include "QDebug"
#include "QSound"
ChooseLevelScene::ChooseLevelScene(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ChooseLevelScene)
{
    ui->setupUi(this);
     this->setFixedSize(320,588);
    this->setWindowTitle("请选择你的关卡");
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    connect(ui->actionquit,&QAction::triggered,[=](){
        this->close();
    });
    myPushButton *BackBtn=new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
   BackBtn->setParent(this);
   BackBtn->move(this->width()-BackBtn->width(),this->height()-BackBtn->height());
   connect(BackBtn,&QPushButton::clicked,[=](){
       QTimer::singleShot(100,this,[=](){
           emit this->chooseScenceBack();
       });
   });

   QSound *chooseSound=new QSound(":/res/TapButtonSound.wav",this);
   QSound *backSound=new QSound(":/res/BackButtonSound.wav",this);

    for(int i=0;i<20;i++){
        myPushButton *menuBtn=new   myPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+i%4*70,130+i/4*70);
        connect(menuBtn,&myPushButton::clicked,[=](){
            chooseSound->play();
            game=new gamescene(i+1);
            game->setGeometry(this->geometry());
            this->hide();
            game->show();
            QString str=QString("你选择的是第%1关").arg(i+1);
            qDebug()<<str;
            connect(game,&gamescene::chooseScenceBack,this,[=](){
                this->setGeometry(game->geometry());
                backSound->play();
                game->hide();
                this->show();
                delete game;
                game=NULL;
            });
        });
        QLabel *label=new QLabel(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(25+i%4*70,130+i/4*70);
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

        //被布局挡住了，设置鼠标的穿透,51号属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }


}

void ChooseLevelScene::paintEvent(QPaintEvent*){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/res/Title.png");
    //可以通过绘图设备来设置图片的大小
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix);
}


ChooseLevelScene::~ChooseLevelScene()
{
    delete ui;
}
